package com.bkq.cb.BLO;

import com.bkq.cb.GetDMY;
import com.bkq.cb.SqliteServices;
import com.bkq.cb.DTO.TransactionDTO;
import com.bkq.cb.DTO.UserDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TransactionBLO {
	SqliteServices services=null;
	Cursor mCursor;
	String TAG;
	GetDMY g = new GetDMY();
	private SQLiteDatabase mDB;
	public TransactionBLO(Context context)
	{
		services = new SqliteServices(context);
	}
	public boolean updateContact(int rowId, String name, String date,String amount)
	{
		ContentValues cv = new ContentValues();
		cv.put("name", name);
		cv.put("date", date);
		cv.put("money", amount);

		return mDB.update("tblTransaction", cv, "idUser" +"="+ rowId, null)>0;
	}
	public boolean insert(TransactionDTO obj)
	{
		String sql="insert into tblTransaction(idUser, name, date, money, type) values('"+obj.IdUser +"','"+obj.Name+"','"+obj.Date+"','"+obj.Money+"','"+obj.Type+"')";		
		boolean check=false;
		
		try
		{
			this.services.exeSQL(sql);
			check=true;
		}
		catch(Exception ex)
		{
			Log.d(TAG, "Insert  Error");
			
		}
		return check;
		
	}
	public float[] getValue(String today,String idUser)
	{
		float[]s=new float[6]; 
		s[0] =0;
		s[1] =0;
		s[2] =0;
		s[3]=0;
		s[4]=0;
		s[5]=0;
		 String y = g.getYear(today);
		  int m = Integer.parseInt(g.getMonth(today));
		  m=m-1;
		services.openDatabase();
		
		mCursor = services.queryData("select * from tblTransaction");
		    
		     if(mCursor.moveToFirst())
		     {
		    	 
				if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
				{
					if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
					{
						
					if(mCursor.getString(5).equals("income"))
					s[0]+=Float.parseFloat(mCursor.getString(4).toString());
					else s[1]+=Float.parseFloat(mCursor.getString(4).toString());
					}
				}
		    	 while(mCursor.moveToNext())
		    	 {
		    		 
		    		 if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
						{
							if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
							{
								
							if(mCursor.getString(5).equals("income"))
							s[0]+=Float.parseFloat(mCursor.getString(4).toString());
							else s[1]+=Float.parseFloat(mCursor.getString(4).toString());
							}
						}
						
		    	 }
		    	
		     }
		     s[2] = s[0] - s[1];
	  mCursor.close();
		    mCursor = services.queryData("select * from tblLoan");
		    
		   
			if(mCursor.moveToFirst())
			     {
			    	 
					if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
					{
						if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
						{
							
						if(mCursor.getString(6).equals("Lend"))
						s[3]+=Float.parseFloat(mCursor.getString(5).toString());
						else s[4]+=Float.parseFloat(mCursor.getString(5).toString());
						}
					}
			    	 while(mCursor.moveToNext())
			    	 {
			    		
			    		 if(g.getYear(mCursor.getString(3)).equals(y)&&mCursor.getString(1).equals(idUser))
							{
								if(g.getMonth(mCursor.getString(3)).equals(String.valueOf(m)))
								{
									
								if(mCursor.getString(6).equals("Lend"))
								s[3]+=Float.parseFloat(mCursor.getString(5).toString());
								else s[4]+=Float.parseFloat(mCursor.getString(5).toString());
								}
							}
							
			    	 }
			    	
			     }
			
			s[5]=s[3]-s[4];
			mCursor.close();
		    services.close();
		return s;
		
	}

}
